package do

import (
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/user/server"
	"micro.user.action.server/models"
	"micro.user.action.server/utils"
	"time"
)

func ConvertAdminUserDBData(req *pb.AdminUserRegistryReq) *models.VsHUser {
	var userName, phone, email string
	userName, _ = utils.Encrypt(req.UserName)
	if req.Phone != "" {
		phone, _ = utils.Encrypt(req.Phone)
	}
	if req.Email != "" {
		email, _ = utils.Encrypt(req.Email)
	}

	return &models.VsHUser{
		UserName:    userName,
		Password:    utils.MD5(req.PassWord),
		Phone:       phone,
		Email:       email,
		Role:        10,
		CreatedTime: time.Now().Unix(),
		UpdatedTime: time.Now().Unix(),
	}
}

func ConvertAdminUserInfo(user *models.VsHUser) *compb.AdminUserInfo {
	var userName, phone, email string
	userName, _ = utils.Decrypt(user.UserName)
	if user.Phone != "" {
		phone, _ = utils.Decrypt(user.Phone)
	}
	if user.Email != "" {
		email, _ = utils.Decrypt(user.Email)
	}

	return &compb.AdminUserInfo{
		UserID:     user.UserId,
		UserName:   userName,
		Phone:      phone,
		Email:      email,
		Role:       user.Role,
		HotelID:    user.HotelId,
		HotelName:  "",
		CreateTime: time.Unix(user.CreatedTime, 0).Format("2006-01-02 15:04:05"),
	}
}
